package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfPointCardApply;
import com.jic.point.model.vo.request.ConfPointCardApplyRequest;
import com.jic.point.model.vo.request.QuantityRequest;
import com.jic.point.model.vo.response.ConfPointCardApplyResponse;
import com.jic.point.service.ConfPointCardApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:32
 */
@RestController
@Slf4j
@RequestMapping("/confPointCardApply")
@Api(tags = " [积分卡批量开卡申请表]控制类-PC")
public class ConfPointCardApplyController {
    @Autowired
    private ConfPointCardApplyService service;


    /**
     * 新增记录
     *
     * @param request 新增[积分卡批量开卡申请表]请求参数
     * @return ConfPointCardApplyResponse 新增[积分卡批量开卡申请表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[积分卡批量开卡申请表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfPointCardApplyRequest request) throws Throwable {
        ConfPointCardApply confPointCardApply = new ConfPointCardApply();
        BeanUtils.copyProperties(request, confPointCardApply);
        long flag = service.save(confPointCardApply);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[积分卡批量开卡申请表]请求参数
     * @return ConfPointCardApplyResponse 新增或修改[积分卡批量开卡申请表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[积分卡批量开卡申请表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfPointCardApplyRequest request) throws Throwable {
        long flag = service.saveOrUpdate(request);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[积分卡批量开卡申请表]请求参数
     * @return ConfPointCardApplyResponse 删除[积分卡批量开卡申请表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[积分卡批量开卡申请表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody ConfPointCardApplyRequest request) throws Throwable {
        ConfPointCardApply confPointCardApply = new ConfPointCardApply();
        BeanUtils.copyProperties(request, confPointCardApply);
        long flag = service.deleteByPrimaryKeyLogically(confPointCardApply);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[积分卡批量开卡申请表]请求参数
     * @return ConfPointCardApplyResponse 修改[积分卡批量开卡申请表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[积分卡批量开卡申请表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody ConfPointCardApplyRequest request) throws Throwable {
        ConfPointCardApply confPointCardApply = new ConfPointCardApply();
        BeanUtils.copyProperties(request, confPointCardApply);
        long flag = service.updateByPrimaryKeySelective(confPointCardApply);
        return RestResult.success(flag);
    }

    /**
     * 将积分卡批量核审
     *
     * @param list 批量核审积分卡
     *             author fengrui
     * @date 2020-10-27 16：06
     */
    @ApiOperation(value = "积分卡审核")
    @PostMapping("listUpdate")
    @ResponseBody
    public RestResult listUpdate(@RequestBody ConfPointCardApplyRequest list) {
        Integer integer = service.listUpdate(list.getList(), list.getAdminOperate());
        if (integer != null && integer > 0) {
            return RestResult.success(null);
        }
        return RestResult.error("9999", "修改失败");
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[积分卡批量开卡申请表]详情请求参数
     * @return ConfPointCardApplyResponse 查询[积分卡批量开卡申请表]详情响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[积分卡批量开卡申请表]详情")
    @ResponseBody
    public RestResult<ConfPointCardApplyResponse> getByPrimaryKey(@Validated @RequestBody ConfPointCardApplyRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            ConfPointCardApply confPointCardApply = new ConfPointCardApply();
            BeanUtils.copyProperties(request, confPointCardApply);
            ConfPointCardApply result = service.getByPrimaryKey(confPointCardApply);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            ConfPointCardApplyResponse bo = new ConfPointCardApplyResponse();
            BeanUtils.copyProperties(result, bo);
            bo.setQuantity(JSONObject.parseArray(result.getQuantity(),QuantityRequest.class));
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new ConfPointCardApplyResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<ConfPointCardApplyResponse>> listPage(@RequestBody ConfPointCardApplyRequest request) throws Throwable {
        return RestResult.success(service.listPageBySelective(request, request.getPageNum(), request.getPageSize()));
    }


    /**
     * CONF_POINT_CARD_APPLY 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [积分卡批量开卡申请表] ")
    @ResponseBody
    public RestResult<List<ConfPointCardApplyResponse>> listConfPointCardApplyAllRecord(@RequestBody ConfPointCardApplyRequest request) throws Throwable {
        ConfPointCardApply param = new ConfPointCardApply();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<ConfPointCardApply> list = service.listAllRecord(param);
        List<ConfPointCardApplyResponse> responseList = list.stream().map(temp -> {
            ConfPointCardApplyResponse bo = new ConfPointCardApplyResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    @PostMapping("/delaying")
    @ApiModelProperty(value = "延期")
    @ResponseBody
    public RestResult<Integer> delaying(@RequestBody ConfPointCardApplyRequest request){
        Integer result = service.delaying(request);
        if (result>0){
            return RestResult.success(result);
        }
        return RestResult.error("9999","延期失败");
    }

    @PostMapping("/firstInstance")
    @ApiModelProperty(value = "批量一审审核")
    @ResponseBody
    public RestResult firstInstance(@RequestBody ConfPointCardApplyRequest request){
        service.firstInstance(request);
        return RestResult.success(null);
    }

}

	
